﻿using System;
using Windows.Media.Capture;
using Windows.Media.Devices;
using Windows.Media.MediaProperties;
using Windows.Storage;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media.Imaging;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace MediaCaptureDemo
{
    /// <summary>
    ///     An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        private bool _flash;
        private bool _focus;
        private bool _iso;
        private RegionsOfInterestControl _regions;
        private SceneModeControl _scene;
        private bool _torch;
        private MediaCapture captureManager;

        public MainPage()
        {
            InitializeComponent();
        }

        private async void InitCamera_Click(object sender, RoutedEventArgs e)
        {
            captureManager = new MediaCapture();
            await captureManager.InitializeAsync();

            // Can only read capabilities after capture device initialized.

            VideoDeviceController videoDeviceController = captureManager.VideoDeviceController;

            SceneModeControl sceneModeControl = _scene = videoDeviceController.SceneModeControl;
            RegionsOfInterestControl regionsOfInterestControl = _regions = videoDeviceController.RegionsOfInterestControl;

            bool isFocusSupported = _focus = videoDeviceController.FocusControl.Supported;
            bool isIsoSpeedSupported = _iso = videoDeviceController.IsoSpeedControl.Supported;
            bool isTorchControlSupported = _torch = videoDeviceController.TorchControl.Supported;
            bool isFlashControlSupported = _flash = videoDeviceController.FlashControl.Supported;

            if (_scene != null)
            {
                foreach (CaptureSceneMode mode in _scene.SupportedModes)
                {
                    string t = mode.ToString();
                }
            }

            if (_regions != null)
            {
                bool autoExposureSupported = _regions.AutoExposureSupported;
                bool autoFocusSupported = _regions.AutoFocusSupported;
                bool autoWhiteBalanceSupported = _regions.AutoWhiteBalanceSupported;
                uint maxRegions = _regions.MaxRegions;
            }
        }

        private async void StartCapturePreview_Click(object sender, RoutedEventArgs e)
        {
            capturePreview.Source = captureManager;
            await captureManager.StartPreviewAsync();
        }

        private async void StopCapturePreview_Click(object sender, RoutedEventArgs e)
        {
            await captureManager.StopPreviewAsync();
        }

        private async void CapturePhoto_Click(object sender, RoutedEventArgs e)
        {
            ImageEncodingProperties imgFormat = ImageEncodingProperties.CreatePng();

            StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(
                "Capture.png",
                CreationCollisionOption.GenerateUniqueName);

            await captureManager.CapturePhotoToStorageFileAsync(imgFormat, file);
            var bmpImage = new BitmapImage(new Uri(file.Path));
            imagePreview.Source = bmpImage;
        }
    }
}